package com.ezhuan.outer.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ezhuan.common.controller.BaseController;
import com.ezhuan.common.util.ConstatFinalUtil;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.system.pojo.AAdver;
import com.ezhuan.system.pojo.AAdverEnum;
import com.ezhuan.system.pojo.ACate;
import com.ezhuan.system.pojo.ACateEnum;
import com.ezhuan.system.pojo.ARegion;
import com.ezhuan.system.pojo.ASuggest;
import com.ezhuan.system.pojo.ASuggestEnum;
import com.ezhuan.system.pojo.AVersion;
import com.ezhuan.system.pojo.AVersionEnum;
import com.ezhuan.system.service.ISystemService;
import com.ezhuan.system.service.impl.SystemServiceImplay;

/**
 * 系统相关的Controller
 * @author wangshSxt
 *
 */
public class SystemOuterController extends BaseController
{
	/* 系统相关的Service */
	private ISystemService systemService = new SystemServiceImplay();
	
	/**
	 * 地区列表
	 * 
	 * @return
	 */
	public void regionList()
	{
		ConstatFinalUtil.LOGGER.info("---regionList---");
		long st = System.currentTimeMillis() ;
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		
		/* 接收rid */
		String rid = this.getPara("rid");
		try
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			/* 根据当前节点查询子节点 */
			condMap.clear();
			condMap.put("parentId", rid);
			List<ARegion> childRegionList = this.systemService.findCondListRegionService(null, condMap);
			/* 查看当前节点对应的子节点 */
			JSONArray regionArr = new JSONArray();
			for (Iterator iterator = childRegionList.iterator(); iterator.hasNext();)
			{
				ARegion regionChild = (ARegion) iterator.next();
				JSONObject regionChildJSON = regionChild.toJSON() ; 
				regionArr.add(regionChildJSON);
			}
			
			/* 查询parentId对应的地区 */
			/* 根据当前节点查询子节点 */
			condMap.clear();
			condMap.put("id", rid);
			ARegion region = this.systemService.findOneRegionService(condMap);
			JSONObject dataResJSON = new JSONObject();
			if(region != null)
			{
				this.systemService.findTreeRegionService(region);
				dataResJSON.put("region", region.toJSON());
			}
			
			dataResJSON.put("regionList", regionArr);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			resultJSON.put("data", dataResJSON);
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("拼装JSON对象出错了;上行数据:{}",this.getParaMap(), e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 建议列表
	 */
	public void suggestList()
	{
		ConstatFinalUtil.LOGGER.info("======suggestList======");
		long st = System.currentTimeMillis() ; 
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		/* 接收参数 */
		String usersId = this.getPara("usersId");
		if (usersId == null)
		{
			usersId = "";
		}
		try
		{
			PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil() ; 
			/* 查询数据库 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("usersId", usersId);
			List<ASuggest> suggestList = this.systemService.findCondListSuggestService(pageInfoUtil, condMap);
			
			JSONArray withdrawArr = new JSONArray();
			for (Iterator iterator = suggestList.iterator(); iterator.hasNext();)
			{
				ASuggest suggest = (ASuggest) iterator.next();
				withdrawArr.add(suggest.toJSON());
			}
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("withdrawList", withdrawArr);
			dataJSON.put("pageInfo", pageInfoUtil.toJSON());
			resultJSON.put("data", dataJSON);
			
			/* 存储结果 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("用户提交资料列表报错了:id:{}", e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 添加建议
	 */
	public void suggestInsertSubmit()
	{
		ConstatFinalUtil.LOGGER.info("======suggestInsertSubmit======");
		long st = System.currentTimeMillis() ; 
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		/* 接收参数 */
		String usersId = this.getPara("usersId");
		String name = this.getPara("name");
		String content = this.getPara("content");
		try
		{
			ASuggest suggest = new ASuggest();
			
			/* 设置参数 */
			suggest.setUsersId(Integer.valueOf(usersId));
			suggest.setName("");
			suggest.setContent(content);
			suggest.setStatus(ASuggestEnum.STATUS_ENABLE.getStatus());
			suggest.setCreateTime(new Date());
			suggest.setUpdateTime(new Date());
			suggest.setPubTime(new Date());
			
			suggest.setCreateTime(new Date());
			suggest.setUpdateTime(new Date());
			suggest.setPubTime(new Date());
			resultJSON = this.systemService.saveOneSuggestService(suggest);
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("添加建议资料详细报错了:参数:{}", this.getParaMap(), e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 广告列表
	 */
	public void adverList()
	{
		ConstatFinalUtil.LOGGER.info("======adverList======");
		long st = System.currentTimeMillis() ; 
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		String atype = this.getPara("atype");
		try
		{
			PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil() ; 
			/* 查询数据库 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("status", AAdverEnum.STATUS_ENABLE.getStatus());
			/* 按照类型查询 */
			condMap.put("atype", Byte.valueOf(atype));
			List<AAdver> adverList = this.systemService.findCondListAdverService(pageInfoUtil, condMap);
			
			JSONArray adverArr = new JSONArray();
			for (Iterator iterator = adverList.iterator(); iterator.hasNext();)
			{
				AAdver adver = (AAdver) iterator.next();
				adverArr.add(adver.toJSON());
			}
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("adverList", adverArr);
			dataJSON.put("pageInfo", pageInfoUtil.toJSON());
			resultJSON.put("data", dataJSON);
			
			/* 存储结果 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("广告列表报错了:id:{}", e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 版本列表
	 */
	public void versionList()
	{
		ConstatFinalUtil.LOGGER.info("======versionList======");
		long st = System.currentTimeMillis() ; 
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		String vers = this.getPara("vers");
		try
		{
			/* 先按照版本名称查询版本号,再查询pubTime>当前版本号的版本 */
			PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil() ; 
			/* 查询数据库 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			/* 按照类型查询 */
			condMap.put("vers", vers);
			AVersion version = this.systemService.findOneVersionService(condMap);
			if(version != null)
			{
				condMap.clear();
				condMap.put("status", AVersionEnum.STATUS_ENABLE.getStatus());
				condMap.put("pubTime", version.getPubTime());
				List<AVersion> versionList = this.systemService.findCondListVersionService(pageInfoUtil, condMap);
				
				JSONArray versionArr = new JSONArray();
				for (Iterator iterator = versionList.iterator(); iterator.hasNext();)
				{
					AVersion versionTemp = (AVersion) iterator.next();
					versionArr.add(versionTemp.toJSON());
				}
				JSONObject dataJSON = new JSONObject();
				dataJSON.put("versionList", versionArr);
				dataJSON.put("pageInfo", pageInfoUtil.toJSON());
				resultJSON.put("data", dataJSON);
				
				/* 存储结果 */
				resultJSON.put("code", "0");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			}else
			{
				/* 查询不到当前版本 */
				resultJSON.put("code", "1");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("广告列表报错了:id:{}", e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
	
	/**
	 * 分类列表
	 */
	public void cateList()
	{
		ConstatFinalUtil.LOGGER.info("======cateList======");
		long st = System.currentTimeMillis() ; 
		/* 存储结果的JSON */
		JSONObject resultJSON = new JSONObject();
		String cateType = this.getPara("cateType");
		try
		{
			/* 先按照分类名称查询分类号,再查询pubTime>当前分类号的分类 */
			PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil() ; 
			/* 查询数据库 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			/* 按照类型查询 */
			condMap.put("cateType", cateType);
			condMap.put("status", ACateEnum.STATUS_ENABLE.getStatus());
			List<ACate> cateList = this.systemService.findCondListCateService(pageInfoUtil, condMap);
			
			JSONArray cateArr = new JSONArray();
			for (Iterator iterator = cateList.iterator(); iterator.hasNext();)
			{
				ACate cateTemp = (ACate) iterator.next();
				cateArr.add(cateTemp.toJSON());
			}
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("cateList", cateArr);
			dataJSON.put("pageInfo", pageInfoUtil.toJSON());
			resultJSON.put("data", dataJSON);
			
			/* 存储结果 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("广告列表报错了:id:{}", e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1") + e.toString());
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consumeTime", (ed - st) + "毫秒");
		/* 封装结果 */
		renderJson(resultJSON);
		System.out.println(resultJSON);
	}
}
